package com.els.test;

import java.io.File;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.elasticsearch.action.index.IndexRequestBuilder;


import com.els.BulkPool;
import com.els.ElsConnection;
import com.els.connection.ConfigElastic;
import com.els.pool.ConfigElasticPool;
import com.els.util.JsonUtil;
import com.google.gson.JsonObject;

public class PaperMain {
//	public static void main(String[] args) {
//		PaperMain pm=new PaperMain();
//		List<JsonObject>results=new Vector<JsonObject>();
//		pm.getList("C:/LearningInfo/Elasticsearch/imp/paper",results);
//		System.out.println("load jsonList finished! size:"+results.size());
//		ElsConnection ec=new ConfigElastic("/config.properties");
//		ec.buildClient();
//		long begin=System.currentTimeMillis();
//		ec.singleBulkIndex("test", "paper", results, null);
//		long end=System.currentTimeMillis();
//		System.out.println("耗时："+(end-begin)/1000+"s");
//		
//	}
//	public static void main(String[] args) {
//
//		PaperMain pm=new PaperMain();
//		List<JsonObject>results=new Vector<JsonObject>();
//		pm.getList("C:/LearningInfo/Elasticsearch/imp/paper",results);
//
//		System.out.println("getList finished,size:"+results.size());
//		
//		BulkPool bp=new ConfigElasticPool("/config.properties");
//		bp.initPool();
//		
//		long begin=System.currentTimeMillis();
//		bp.concurrentBulkIndex("test", "paper", null, results);
//
//		long end=System.currentTimeMillis();
//		System.out.println("耗时："+(end-begin)/1000+"s");
//		
//		
//		
//	}
	
	public static void main(String[] args) {

		PaperMain pm=new PaperMain();
		List<JsonObject>results=new Vector<JsonObject>();
		List<IndexRequestBuilder>requestList=new Vector<IndexRequestBuilder>();
		pm.getList("C:/LearningInfo/Elasticsearch/imp/paper",results);
		ElsConnection ec=new ConfigElastic("/config.properties");
		ec.buildClient();
		System.out.println("getList finished,size:"+results.size());
		for (JsonObject row : results) {
			IndexRequestBuilder request = ec.prepareInsertOne("test", "paper", null, row);
			requestList.add(request);
		}
		BulkPool bp=new ConfigElasticPool("/config.properties");
		bp.initPool();
		
		long begin=System.currentTimeMillis();
		bp.concurrentBulk(requestList);

		long end=System.currentTimeMillis();
		System.out.println("耗时："+(end-begin)/1000+"s");
		
		
		
	}
	
//	public static void main(String[] args) {
//
//		ExecutorService fixedThreadPool=Executors.newFixedThreadPool(3);
//		for(int i=0;i<10;i++){
//			fixedThreadPool.execute(new Runnable() {
//				public void run() {
//					System.out.println("type");
//					try {
//						Thread.sleep(1000);
//					} catch (InterruptedException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//				}
//			});
//		}
//		// 关闭启动线程
//		fixedThreadPool.shutdown();
//        // 等待子线程结束，再继续执行下面的代码
//		try {
//			fixedThreadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//        System.out.println("all thread complete");
//		
//		
//	}
	
	public List<JsonObject> getList(String dirpath,List<JsonObject> results){
		File dir=new File(dirpath);
		for (File file : dir.listFiles()) {
			if(file.isDirectory()){
				getList(file.getAbsolutePath(), results);
			}else {
				JsonObject object=JsonUtil.traslateToJson(file.getAbsolutePath());
//				object.addProperty("_id", file.getName().replace(".txt", ""));
				results.add(object);
			}
		}
		return results;
	}
}
